<?php
abstract class m_form_extends_SuccessAction
{
    private $formValues;
    private $structure;
    private $parameters = array();
    private $context;

    /**
     * Set form structure document
     *
     * @param m_form_documents_Structure $formValues
     * @return m_form_extends_SuccessAction
     */
    public function setStructure($structure)
    {
        if(is_object($structure) && $structure->getModel() == 'modules_form/structure' )
        {
            $this->structure = $structure;
        }
        return $this;
    }

    /**
     * @return m_form_documents_Structure
     */
    public function getStructure()
    {
        return $this->structure;
    }
    /**     
     * @return integer
     */
    public function getStructureId()
    {
        if(isset($this->structure))
        {
            return intval($this->structure->getUniqueId());
        }
        return null;
    }

    /**
     * Set form values for success actions
     *
     * @param array $formValues
     * @return m_form_extends_SuccessAction
     */
    public function setFormValues($formValues)
    {
        $this->formValues = $formValues;
        return $this;
    }

    /**
     * Set parameters from xml form structure
     *
     * @param array $value
     * @return m_form_extends_SuccessAction
     */
    public function setParameters($value)
    {
        $this->parameters = $value;
        return $this;
    }

    public function getParameters()
    {
        return $this->parameters;
    }

    public function getParameter($key, $default = null)
    {
        if($this->hasParameter($key))
        {
            if(is_string($this->parameters[$key]))
            {
                return $this->formatString($this->parameters[$key]);
            }
            return $this->parameters[$key];
        }
        return $default;
    }

    public function formatString($string)
    {
        preg_match_all('`\{(.*?)\}`', $string, $keyWords);

        if(isset($keyWords[0]) && is_array($keyWords[0]))
        {
            foreach($keyWords[1] as $k => $keyword)
            {
                if($this->hasFormValue($keyword))
                {
                    $string = str_replace($keyWords[0][$k], $this->getFormValue($keyword), $string);
                }
            }
        }

        return $string;
    }

    public function hasParameter($key)
    {
        return (isset($this->parameters[$key]) && !is_null($this->parameters[$key]));
    }

    public function getFormValues()
    {
        $values = $this->formValues;

        if(isset($values['spinner']))
        {
            unset($values['h_o_n_e_y_p_o_t']);
            unset($values['timestamp']);
            unset($values['spinner']);
        }

        return $values;
    }

    public function getFormValue($key)
    {
        if($this->hasFormValue($key))
        {
            return $this->formValues[$key];
        }
        return null;
    }

    public function hasFormValue($key)
    {
        return (isset($this->formValues[$key]) && !is_null($this->formValues[$key]));
    }

    public function setContextWebsite($website)
    {
        $this->context['website'] = $website;
        return $this;
    }

    public function setContextPage($page)
    {
        $this->context['page'] = $page;
        return $this;
    }

    public function getContextWebsite()
    {
        return $this->context['website'];
    }

    public function getContextPage()
    {
        return $this->context['page'];
    }

    abstract public function execute();
}